package gob.pe.mtpe.util.funciones;

////import org.apache.commons.logging.Log;
////import org.apache.commons.logging.LogFactory;


import gob.pe.mtpe.ibatis.despachadores.SeguroDespatch;
import gob.pe.mtpe.struts.form.SeguroVidaForm;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;


/**
 * Servlet que atiene peticiones especificas por fragmentos de texto, html o xml.
 *
 */
public class XMLService   extends HttpServlet
{
    private static Logger depurador = Logger.getLogger(XMLService.class.getName());
    
	
    public void doGet(HttpServletRequest request,HttpServletResponse response)
      throws ServletException,IOException
    {
        //header params
        response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("text/html; charset=iso-8859-1");

        //method 
        String metodo = request.getParameter("metodo");
        if ((metodo == null) || ! metodo.startsWith("request")) {
        	return;
        }
        String resul;

        //method calling
        Method method = null;
        try {
            method = this.getClass().getMethod(metodo, new Class[] { HttpServletRequest.class, HttpServletResponse.class });
            resul = "OK" + (String) method.invoke(this, new Object[] { request, response });
        }catch(InvocationTargetException ite){        	        	
		   String _exmsg = ite.getTargetException().getMessage();
		   if(_exmsg!=null && _exmsg.indexOf("ORA-2")>=0){
		   	_exmsg = _exmsg.substring(_exmsg.indexOf("ORA-")+10,_exmsg.indexOf("\n"));
		   }
           resul = "Error de sistema:\n" + _exmsg;
           //System.out.println("Exception msg:" + _exmsg);        	
        }catch (Exception ex) {
           resul = "Error de sistema:\n" + ex.getMessage();
        }

        //sending response.
        response.getWriter().write(resul);
    }
    

    public String requestobtNomCompAseguradora(HttpServletRequest request, HttpServletResponse response)
    throws Exception{
    	//System.out.println("SERVICE obtNomCompAseguradora=====>");
    	try {
    		
    		StringBuffer text      = new StringBuffer();		
    		String ruc = (String)request.getParameter("ruc");
    		//depurador.info("ruccccccccc "+ruc);
    		SeguroDespatch seguroDespatch = new SeguroDespatch();
    		String nombre =(String)seguroDespatch.obtEmpAse(ruc);
    		return nombre;
    	}
    	catch (Exception e) {			
    		e.printStackTrace();
    		throw new ServletException(e);
    	}
    }
    
    
}
